import React from 'react';
import { Button, message } from 'antd';
import { DownloadOutlined } from '@ant-design/icons';

class ExportButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isExport: false
    }
  }
  /* 导出数据 */
  onExport = async () => {
    try {
      const { query, request } = this.props;
      if (!request) throw new Error('缺少导出方法！');
      this.setState({ isExport: true });
      const res = await request(query);
      res && res.filePath ? window.open(res.filePath) : message.warning('没有数据！')
    } catch (e) {
      console.log(e);
      e.message ? message.error(e.message) : null;
    } finally {
      this.setState({ isExport: false });
    }
  }
  render() {
    const { onExport } = this;
    const { isExport } = this.state;
    return (
      <Button
        style={{ marginRight: 20 }}
        type='primary'
        icon={<DownloadOutlined />}
        loading={isExport}
        onClick={onExport}>导出</Button>
    )
  }
}

export default ExportButton